﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;

namespace AreaEdit.AreaSet
{
	public partial class InsideAreaForm : System.Windows.Forms.Form
	{
		public InsideAreaForm(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			this.tbHouseName.Text = "B";
			this.tbHouseNum.Text = "5";
			this.tbUnit.Text = "1-A";
			this.tbBuildingNum.Text = "101";
			this.m_roomInfo = default(RoomInfo);
			this.m_roomInfo.m_houseName = this.tbHouseName.Text;
			this.m_roomInfo.m_houseNum = this.tbHouseNum.Text;
			this.m_roomInfo.m_unit = this.tbUnit.Text;
			this.m_roomInfo.m_buildingNum = this.tbBuildingNum.Text;
			if (!this.ReadData())
			{
				base.Location = RevitVersionFuncs.GetRevitTopLeftPoint(cmdData);
				YJKMessageBox.Show("读取套内面积标注信息文件失败。");
			}
		}

		private void SaveData()
		{
			string userDirectoryFile = Common.GetUserDirectoryFile("\\InsideAreaLabelInfo.dat");
			FileStream fileStream = new FileStream(userDirectoryFile, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_labelInfo.m_labelHouseName);
			streamWriter.WriteLine(this.m_labelInfo.m_labelHouseNum);
			streamWriter.WriteLine(this.m_labelInfo.m_labelUnit);
			streamWriter.WriteLine(this.m_labelInfo.m_labelBuildingNum);
			streamWriter.WriteLine(this.m_labelInfo.m_labelInsideArea);
			streamWriter.WriteLine(this.m_labelInfo.m_labelLanguage);
			streamWriter.WriteLine(this.m_RangeType);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string userDirectoryFile = Common.GetUserDirectoryFile("\\InsideAreaLabelInfo.dat");
			FileStream fileStream = new FileStream(userDirectoryFile, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.m_labelInfo = default(InsideAreaLableInfo);
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new Point(x, y);
				this.m_labelInfo.m_labelHouseName = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_labelHouseNum = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_labelUnit = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_labelBuildingNum = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_labelInsideArea = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_labelLanguage = streamReader.ReadLine();
				this.m_labelInfo.m_RangeType = "KeepWarm";
				this.m_labelInfo.m_RangeType = streamReader.ReadLine();
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		private void InsideAreaForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void InsideAreaForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void InsideAreaForm_Deactivate(object sender, EventArgs e)
		{
			this.m_roomInfo.m_houseName = this.tbHouseName.Text;
			this.m_roomInfo.m_houseNum = this.tbHouseNum.Text;
			this.m_roomInfo.m_unit = this.tbUnit.Text;
			this.m_roomInfo.m_buildingNum = this.tbBuildingNum.Text;
		}

		private void btnLabelSet_Click(object sender, EventArgs e)
		{
			InsideAreaSetForm insideAreaSetForm = new InsideAreaSetForm(this.m_labelInfo);
			if (DialogResult.OK == insideAreaSetForm.ShowDialog())
			{
				this.m_labelInfo = insideAreaSetForm.m_labelInfo;
				this.m_reset = true;
				this.m_RangeType = insideAreaSetForm.RangeType;
				this.KeyEsc();
			}
		}

		private void InsideAreaForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			InsideAreaForm.SendMessage(CmdInsideArea._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			InsideAreaForm.keybd_event(bVk, 0, dwFlags, 0);
			InsideAreaForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void InsideAreaForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.KeyEsc();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public RoomInfo m_roomInfo;

		public InsideAreaLableInfo m_labelInfo;

		public bool m_reset;

		public string m_RangeType = "KeepWarm";
	}
}
